package org.huangrui.algorithm;

/**
 * @Author hr
 * @Create 2025-11-03 13:05
 * 283. 移动零
 * https://leetcode.cn/problems/move-zeroes/description/
 * 快慢相等值不变，慢针不动快针走；
 * 快慢不等值改变，快针赋值给慢针(快慢值互换)，慢针向前一步走，快针向前一步走；
 **/
public class T022LeetCode283 {
    public void moveZeroes(int[] nums) {
        if (nums == null) {
            return;
        }
        int fast = 0, slow = 0;
        while (fast < nums.length) {
            if (nums[fast] != 0) {
                //1 通过交换，快针赋值给慢针
                int temp = nums[slow];
                nums[slow] = nums[fast];
                nums[fast] = temp;
                //2 慢针向前一步走
                slow++;
            }
            //3 快针向前一步走
            fast++;
        }
        //4 自己测试，输出结果
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
    }

    public static void main(String[] args) {
        T022LeetCode283 test = new T022LeetCode283();
        test.moveZeroes(new int[]{0, 1, 0, 3, 12});
    }
}
